package org.sevaapp.security.domain;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Transient;

import org.sevaapp.dao.DaoException;
import org.sevaapp.domain.BaseDomainObject;
import org.sevaapp.security.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;

@MappedSuperclass
public abstract class BaseUser extends BaseDomainObject {
	
	@Id
    @Column(name="username")
	private String loginId;
	
	private String password;
	
	private boolean enabled;

	private String userFullName;
	
	private String emailAddress;
	
	public int getReferralId() {
		return referralId;
	}

	public void setReferralId(int referralId) {
		this.referralId = referralId;
	}

	private int referralId;

//	@OneToMany(mappedBy = "parent", targetEntity=Authority.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
//	@OneToMany(mappedBy="parent",targetEntity=Authority.class, fetch=FetchType.EAGER)
//	@OneToMany(targetEntity=Authority.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@OneToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL, mappedBy="parent")
	private List<Authority> roles;

	/**
	 * @return the roles
	 */
	public List<Authority> getRoles() {
		return roles;
	}

	/**
	 * @param roles the roles to set
	 */
	public void setRoles(List<Authority> roles) {
		this.roles = roles;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}

	public String getUserFullName() {
		return userFullName;
	}

	public void setUserFullName(String userFullName) {
		this.userFullName = userFullName;
	}

	public String getLoginId() {
		return loginId;
	}

	public void setLoginId(String loginId) {
		this.loginId = loginId;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Autowired
	@Transient
	private UserDao dao;

	// CRUD Methods
	public void add() {
		try {
			dao.add((User) this);
		} catch (DaoException ex) {
			ex.printStackTrace();
		}
	}

	public void add(User inputData) {
		try {
			dao.add((User) inputData);
		} catch (DaoException ex) {
			ex.printStackTrace();
		}
	}

	public void delete(String id) {
		try {
			dao.delete(id);
		} catch (DaoException ex) {
			ex.printStackTrace();
		}
	}

	public void update(User inputData) {
		try {
			dao.update(inputData);
		} catch (DaoException ex) {
			ex.printStackTrace();
		}
	}

	public User load(String id) {
		User newDomainObj = null;
		try {
			newDomainObj = dao.load(id);
		} catch (DaoException ex) {
			ex.printStackTrace();
		}
		return newDomainObj;
	}
}
